#include "EncryptionImpl.h"


EncryptionImpl::EncryptionImpl(void)
{
	m_mid_encrypt = 0;
	m_mid_decrypt = 0;
	m_mid_encrypt_rtcp = 0;
	m_mid_decrypt_rtcp = 0;
}

EncryptionImpl::~EncryptionImpl(void)
{
}

bool EncryptionImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_encrypt = (env)->GetMethodID(m_cls, "encrypt", "(I[B)[B");
		if(!m_mid_encrypt)
			break;

		m_mid_decrypt = (env)->GetMethodID(m_cls, "decrypt", "(I[B)[B");
		if(!m_mid_encrypt)
			break;

		m_mid_encrypt_rtcp = (env)->GetMethodID(m_cls, "encrypt_rtcp", "(I[B)[B");
		if(!m_mid_encrypt)
			break;

		m_mid_decrypt_rtcp = (env)->GetMethodID(m_cls, "decrypt_rtcp", "(I[B)[B");
		if(!m_mid_encrypt)
			break;

		result = true;

	} while (false);

	return result;
}

void EncryptionImpl::encrypt(
	int channel,
	unsigned char* in_data,
	unsigned char* out_data,
	int bytes_in,
	int* bytes_out)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jbyteArray in_array = JniHelper::c2j_bytes(env, (unsigned char*)in_data, bytes_in);
		jbyteArray out_array = (jbyteArray)(env)->CallObjectMethod(m_obj, m_mid_encrypt, channel, in_array);
		if(out_array != 0)
		{
			*bytes_out = (env)->GetArrayLength(out_array);
			if(*bytes_out > 0)
			{
				JniHelper::c2j_bytes(env, out_data, *bytes_out);
			}
		}

		after(env);
	}
}

void EncryptionImpl::decrypt(
	int channel,
	unsigned char* in_data,
	unsigned char* out_data,
	int bytes_in,
	int* bytes_out)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jbyteArray in_array = JniHelper::c2j_bytes(env, (unsigned char*)in_data, bytes_in);
		jbyteArray out_array = (jbyteArray)(env)->CallObjectMethod(m_obj, m_mid_decrypt, channel, in_array);
		if(out_array != 0)
		{
			*bytes_out = (env)->GetArrayLength(out_array);
			if(*bytes_out > 0)
			{
				JniHelper::c2j_bytes(env, out_data, *bytes_out);
			}
		}

		after(env);
	}
}

void EncryptionImpl::encrypt_rtcp(
	int channel,
	unsigned char* in_data,
	unsigned char* out_data,
	int bytes_in,
	int* bytes_out)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jbyteArray in_array = JniHelper::c2j_bytes(env, (unsigned char*)in_data, bytes_in);
		jbyteArray out_array = (jbyteArray)(env)->CallObjectMethod(m_obj, m_mid_encrypt_rtcp, channel, in_array);
		if(out_array != 0)
		{
			*bytes_out = (env)->GetArrayLength(out_array);
			if(*bytes_out > 0)
			{
				JniHelper::c2j_bytes(env, out_data, *bytes_out);
			}
		}

		after(env);
	}
}

void EncryptionImpl::decrypt_rtcp(
	int channel,
	unsigned char* in_data,
	unsigned char* out_data,
	int bytes_in,
	int* bytes_out)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jbyteArray in_array = JniHelper::c2j_bytes(env, (unsigned char*)in_data, bytes_in);
		jbyteArray out_array = (jbyteArray)(env)->CallObjectMethod(m_obj, m_mid_decrypt_rtcp, channel, in_array);
		if(out_array != 0)
		{
			*bytes_out = (env)->GetArrayLength(out_array);
			if(*bytes_out > 0)
			{
				JniHelper::c2j_bytes(env, out_data, *bytes_out);
			}
		}

		after(env);
	}
}